package day06.concurrent03;

/**
 * synchronized 线程间通信机制
 */
public class Demo05 {
    static String name = "小明";
    static String gender = "男";

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    synchronized (Demo05.class){
                        try {
                            System.out.println(name+"#"+gender);
                            Demo05.class.notify();
                            Demo05.class.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    synchronized (Demo05.class){
                        try {
                            if("小明".equals(name)){
                                name = "小花";
                                gender = "女";
                            }else{
                                name = "小明";
                                gender = "男";
                            }
                            Demo05.class.notify();
                            Demo05.class.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }
}
